package org.steven.crm.core.service.salebill;

import com.steven.common.utils.DateTimeUtil;
import com.steven.sequence.generater.SnowflakeGenerator;
import com.steven.xdb.dbmanager.DBManager;
import com.steven.xdb.pagination.query.AbstractQueryDataList;
import org.apache.commons.collections.CollectionUtils;
import org.steven.crm.core.dto.salebill.SaveSaleBillProductRequest;
import org.steven.crm.core.service.BaseService;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author wuzw
 * @Date 2024/6/21 16:31
 * @Version 1.0
 */
public class SaleBillProductService extends BaseService {
    @Override
    protected String getTable() {
        return "saleBillProduct";
    }

    @Override
    protected AbstractQueryDataList getQueryDataList() {
        return null;
    }

    @Override
    protected String getDataSourceName() {
        return null;
    }

    public void batchSave(String saleBillId, Collection<SaveSaleBillProductRequest> products) throws Exception {
        List<String> curProductIds = readProductIdsBySaleBillId(saleBillId);
        List<String> newProductIds = products.stream().map(e -> e.getProductId()).collect(Collectors.toList());

        Map<String, SaveSaleBillProductRequest> productMap = products.stream().collect(Collectors.toMap(e -> e.getProductId(), e -> e));

        Collection<String> keepProductIds = CollectionUtils.intersection(newProductIds, curProductIds);
        Collection<String> deleteProductIds = CollectionUtils.subtract(curProductIds, keepProductIds);
        Collection<String> addProductIds = CollectionUtils.subtract(newProductIds, keepProductIds);

        if (deleteProductIds.size() > 0) {
            String sql = "update " + getTable() + " set deleted = ? where saleBillId = ? and productId in ("
                    + deleteProductIds.stream().collect(Collectors.joining(",")) + ")";
            List<String> p = new ArrayList<>();
            p.add(System.currentTimeMillis() + "");
            p.add(saleBillId);
            DBManager.update(getSession(), sql, p);
        }

        if (addProductIds.size() > 0) {
            List<Map<String, String>> addDatas = new ArrayList<>();
            for (String productId : addProductIds) {
                SaveSaleBillProductRequest product = productMap.get(productId);
                Map<String, String> addData = new HashMap<>();
                addData.put("id", SnowflakeGenerator.getIndex());
                addData.put("saleBillId", saleBillId);
                addData.put("productId", productId);
                addData.put("name", product.getName());
                addData.put("num", product.getNum());
                addData.put("price", product.getPrice());
                addData.put("productAmount", new BigDecimal(product.getNum())
                        .multiply(new BigDecimal(product.getPrice())).toPlainString());
                addData.put("remark", product.getRemark());
                addData.put("addTime", DateTimeUtil.getCurrentDateTime());
                addData.put("updateTime", DateTimeUtil.getCurrentDateTime());
                addDatas.add(addData);
            }
            DBManager.batchInsert(getSession(), getTable(), addDatas);
        }

        if (keepProductIds.size() > 0) {
            for (String productId : keepProductIds) {
                SaveSaleBillProductRequest product = productMap.get(productId);
                Map<String, String> updateK = new HashMap<>();
                updateK.put("saleBillId", saleBillId);
                updateK.put("productId", productId);

                Map<String, String> updateData = new HashMap<>();
                updateData.put("num", product.getNum());
                updateData.put("price", product.getPrice());
                updateData.put("productAmount", new BigDecimal(product.getNum())
                        .multiply(new BigDecimal(product.getPrice())).toPlainString());
                updateData.put("remark", product.getRemark());

                DBManager.update(getSession(), getTable(), updateK, updateData);
            }
        }
    }

    public List<String> readProductIdsBySaleBillId(String saleBillId) throws Exception {
        List<Map<String, String>> roles = readBySaleBillId(saleBillId);
        return roles.stream().map(e -> e.get("productId")).collect(Collectors.toList());
    }

    public List<Map<String, String>> readBySaleBillId(String saleBillId) throws Exception {
        Map<String, String> k = new HashMap<>();
        k.put("saleBillId", saleBillId);
        k.put("deleted", "0");
        return DBManager.query(getSession(), getTable(), k);
    }
}
